# from docx import Document
#
# def docx_tables_to_markdown(docx_path):
#     doc = Document(docx_path)
#     for table in doc.tables:
#         md = []
#         for i, row in enumerate(table.rows):
#             cells = [cell.text.strip() for cell in row.cells]
#             md.append('| ' + ' | '.join(cells) + ' |')
#             if i == 0:
#                 md.append('| ' + ' | '.join(['---'] * len(cells)) + ' |')
#         print('\n'.join(md), '\n')
#
# docx_tables_to_markdown("D:\\hbyt\\AI智能投标\\激励及绩效管理_v1.0_2201.docx")
#
# from docx import Document
#
# def docx_tables_to_markdown(docx_path, output_md_path):
#     doc = Document(docx_path)
#     with open(output_md_path, 'w', encoding='utf-8') as f:
#         for table_index, table in enumerate(doc.tables):
#             md_lines = []
#             for i, row in enumerate(table.rows):
#                 cells = [cell.text.strip() for cell in row.cells]
#                 md_lines.append('| ' + ' | '.join(cells) + ' |')
#                 if i == 0:
#                     md_lines.append('| ' + ' | '.join(['---'] * len(cells)) + ' |')
#             f.write(f'### 表格 {table_index + 1}\n\n')
#             f.write('\n'.join(md_lines))
#             f.write('\n\n')
#
# # 用法示例
# docx_tables_to_markdown('D:\\hbyt\\AI智能投标\\激励及绩效管理_v1.0_2201.docx', 'output.md')
#

from docx import Document
def clean_cell_text(text):
    return text.replace('\n', ' ').replace('\r', ' ').strip()
def docx_tables_to_markdown(docx_path, output_md_path):
    doc = Document(docx_path)
    with open(output_md_path, 'w', encoding='utf-8') as f:
        for table_index, table in enumerate(doc.tables):
            md_lines = []
            for i, row in enumerate(table.rows):
                cells = [cell.text.replace('\n', ' ').replace('\r', ' ').strip() for cell in row.cells]
                md_lines.append('| ' + ' | '.join(cells) + ' |')
                if i == 0:
                    md_lines.append('| ' + ' | '.join(['---'] * len(cells)) + ' |')
            f.write(f'### 表格 {table_index + 1}\n\n')
            f.write('\n'.join(md_lines))
            f.write('\n\n')
# 用法示例
# docx_tables_to_markdown('D:\\hbyt\\AI智能投标\\激励及绩效管理_v1.0_2201.docx', 'output1.md')

docx_tables_to_markdown('D:\\hbyt\\AI智能投标\\2025_04_23_Word\\2025_04_23_Word\\Word\\1.0\\中文\\ITO服务能力\\ITO行业发展趋势_v1.0_2211.docx', 'output2.md')
